package com.example.student_manager_backend.controller;

import com.example.student_manager_backend.entity.OperationLog;
import com.example.student_manager_backend.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/logs")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    @GetMapping
    public ResponseEntity<Page<OperationLog>> findLogs(
            Specification<OperationLog> spec,
            @PageableDefault(size = 10) Pageable pageable) {
        Page<OperationLog> logs = operationLogService.findLogs(spec, pageable);
        return ResponseEntity.ok(logs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<OperationLog> findLogById(@PathVariable Long id) {
        OperationLog log = operationLogService.findLogById(id);
        if (log != null) {
            return ResponseEntity.ok(log);
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteLog(@PathVariable Long id) {
        operationLogService.deleteLog(id);
        return ResponseEntity.ok().build();
    }
} 